package com.lagou.service;

import com.lagou.bean.Position;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service("positionService")
public class PositionService {
    @Autowired
    private SolrClient  solrClient;
    // 添加数据
    public   void  addPosition(Position position){
        try {
            solrClient.addBean(position);
            solrClient.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
    }
    public List<Position> queryPosition(String query){
        SolrQuery  solrQuery = new SolrQuery();
        // 设置默认搜索域
        solrQuery.set("df","positionName");
        solrQuery.setQuery(query);
        // 高亮显示设置
        solrQuery.setHighlight(true);
        // 添加高亮显示的域
        solrQuery.addHighlightField("companyName positionAdvantage");
        solrQuery.setHighlightSimplePre("<font color='red'>");
        solrQuery.setHighlightSimplePost("</font>");
//        solrQuery.setHighlightSnippets(3);
//        solrQuery.setHighlightFragsize(2);
        try {
            QueryResponse queryResponse = solrClient.query(solrQuery);
            if (queryResponse == null){
                return  null;
            }
            // 获取高亮
            Map<String,Map<String,List<String>>> hilightMap = queryResponse.getHighlighting();
            // 获取书的列表
            List<Position>  positions = queryResponse.getBeans(Position.class);
            // 赋值高亮信息
            for (Position position : positions){
                List<String> list = hilightMap.get(position.getId()).get("positionAdvantage");
                if(!CollectionUtils.isEmpty(list)){
                    position.setPositionAdvantage(list.get(0));
                }
                list = hilightMap.get(position.getId()).get("companyName");
                if(!CollectionUtils.isEmpty(list)){
                    position.setCompanyName(list.get(0));
                }
            }
            return  positions;
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  null;
    }


    public List<Position> searchPos(String qurey) throws IOException {

        SolrQuery  solrQuery = new SolrQuery();
        // 设置默认搜索域
        solrQuery.setQuery(qurey);
        try {
            QueryResponse queryResponse = solrClient.query(solrQuery);
            if (queryResponse == null){
                return  null;
            }
            // 获取书的列表
            return queryResponse.getBeans(Position.class);
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        return null;
    }
}
